package o;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class wj implements sd<Bitmap> {
    public static final sb<CompressFormat> ˎ = sb.ˏ("com.bumptech.glide.load.resource.bitmap.BitmapEncoder.CompressionFormat");
    public static final sb<Integer> ˏ = sb.ˎ("com.bumptech.glide.load.resource.bitmap.BitmapEncoder.CompressionQuality", Integer.valueOf(90));
    @Nullable
    private final ud ˊ;

    public /* synthetic */ boolean ˋ(@NonNull Object obj, @NonNull File file, @NonNull sc scVar) {
        return ॱ((tx) obj, file, scVar);
    }

    public wj(@NonNull ud udVar) {
        this.ˊ = udVar;
    }

    @Deprecated
    public wj() {
        this.ˊ = null;
    }

    public boolean ॱ(@NonNull tx<Bitmap> txVar, @NonNull File file, @NonNull sc scVar) {
        boolean z;
        Throwable e;
        Throwable th;
        Bitmap bitmap = (Bitmap) txVar.ˋ();
        CompressFormat ˋ = ˋ(bitmap, scVar);
        aaw.ॱ("encode: [%dx%d] %s", Integer.valueOf(bitmap.getWidth()), Integer.valueOf(bitmap.getHeight()), ˋ);
        try {
            long ॱ = aaq.ॱ();
            int intValue = ((Integer) scVar.ॱ(ˏ)).intValue();
            OutputStream fileOutputStream;
            try {
                fileOutputStream = new FileOutputStream(file);
                try {
                    if (this.ˊ != null) {
                        fileOutputStream = new sj(fileOutputStream, this.ˊ);
                    }
                    bitmap.compress(ˋ, intValue, fileOutputStream);
                    fileOutputStream.close();
                    z = true;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2) {
                        }
                    }
                } catch (IOException e3) {
                    e = e3;
                    try {
                        if (Log.isLoggable("BitmapEncoder", 3)) {
                            Log.d("BitmapEncoder", "Failed to encode Bitmap", e);
                        }
                        if (fileOutputStream == null) {
                            try {
                                fileOutputStream.close();
                                z = false;
                            } catch (IOException e4) {
                                z = false;
                            }
                        } else {
                            z = false;
                        }
                        if (Log.isLoggable("BitmapEncoder", 2)) {
                            Log.v("BitmapEncoder", "Compressed with type: " + ˋ + " of size " + aau.ˏ(bitmap) + " in " + aaq.ˎ(ॱ) + ", options format: " + scVar.ॱ(ˎ) + ", hasAlpha: " + bitmap.hasAlpha());
                        }
                        aaw.ˏ();
                        return z;
                    } catch (Throwable th2) {
                        th = th2;
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e5) {
                            }
                        }
                        throw th;
                    }
                }
            } catch (IOException e6) {
                e = e6;
                fileOutputStream = null;
                if (Log.isLoggable("BitmapEncoder", 3)) {
                    Log.d("BitmapEncoder", "Failed to encode Bitmap", e);
                }
                if (fileOutputStream == null) {
                    z = false;
                } else {
                    fileOutputStream.close();
                    z = false;
                }
                if (Log.isLoggable("BitmapEncoder", 2)) {
                    Log.v("BitmapEncoder", "Compressed with type: " + ˋ + " of size " + aau.ˏ(bitmap) + " in " + aaq.ˎ(ॱ) + ", options format: " + scVar.ॱ(ˎ) + ", hasAlpha: " + bitmap.hasAlpha());
                }
                aaw.ˏ();
                return z;
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                throw th;
            }
            if (Log.isLoggable("BitmapEncoder", 2)) {
                Log.v("BitmapEncoder", "Compressed with type: " + ˋ + " of size " + aau.ˏ(bitmap) + " in " + aaq.ˎ(ॱ) + ", options format: " + scVar.ॱ(ˎ) + ", hasAlpha: " + bitmap.hasAlpha());
            }
            aaw.ˏ();
            return z;
        } catch (Throwable th4) {
            aaw.ˏ();
        }
    }

    private CompressFormat ˋ(Bitmap bitmap, sc scVar) {
        CompressFormat compressFormat = (CompressFormat) scVar.ॱ(ˎ);
        if (compressFormat != null) {
            return compressFormat;
        }
        if (bitmap.hasAlpha()) {
            return CompressFormat.PNG;
        }
        return CompressFormat.JPEG;
    }

    @NonNull
    public rw ˊ(@NonNull sc scVar) {
        return rw.TRANSFORMED;
    }
}
